package cn.itsource.controller;

import cn.itsource.service.IFileService;
import cn.itsource.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author : 张金铭
 * @description :
 * @create :2022-11-14 16:04:00
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private IFileService fileService;


    @PostMapping("/upload")
    public JsonResult updateFile(MultipartFile file){
        String uploadFile = fileService.uploadFile(file);
//        log.error(uploadFile);
        if (uploadFile!=null){
            return JsonResult.success(uploadFile);
        }else {
            return JsonResult.error("上传失败");
        }
    }

    @DeleteMapping("/deleteFile/{fileName}")
    public JsonResult delete(@PathVariable("fileName") String fileName){
//        log.error(fileName);
        if (fileService.deleteFile(fileName)) {
            return JsonResult.success("OK");
        }
        return JsonResult.error("删除失败");
    }
}
